package com.dao;

import java.util.ArrayList;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import org.hibernate.Transaction;

import com.listener.HibernateListener;
import com.pojo.Contact;

import com.pojo.Programme;

import com.vo.ProgrammeVO;

public class ProgrammeDAO {

	@SuppressWarnings("unchecked")
	public List<ProgrammeVO> getByFilterName(String filterName, String isActive) {
		List<ProgrammeVO> list = new ArrayList<ProgrammeVO>();

		// get hibernate session from the servlet context

		Session session = HibernateListener.getSessionFactory().openSession();
		session.beginTransaction();
		String sql;
		if (isActive.equals("No")) {
			sql = "select new com.vo.ProgrammeVO(a.programmeId, a.programmeName, a.description, a.isActive, a.contact.mobilePhone) "
					+ " from Programme as a where a.programmeName like '"
					+ filterName + "%'";
		} else {
			sql = "select new com.vo.ProgrammeVO(a.programmeId, a.programmeName, a.description, a.isActive, a.contact.mobilePhone) "
					+ " from Programme as a where a.programmeName like '"
					+ filterName + "%' and a.isActive like '" + isActive + "'";
		}

		Query query = session.createQuery(sql);
		list = query.list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<ProgrammeVO> getAll(String isActive) {
		List<ProgrammeVO> list = new ArrayList<ProgrammeVO>();

		// get hibernate session from the servlet context

		Session session = HibernateListener.getSessionFactory().openSession();
		session.beginTransaction();
		String sql;
		if (isActive.equals("No")) {
			sql = "select new com.vo.ProgrammeVO(a.programmeId, a.programmeName, a.description, a.isActive, a.contact.mobilePhone) "
					+ " from Programme as a";
		} else {
			sql = "select new com.vo.ProgrammeVO(a.programmeId, a.programmeName, a.description, a.isActive, a.contact.mobilePhone) "
					+ " from Programme as a where a.isActive like '"
					+ isActive
					+ "'";
		}

		Query query = session.createQuery(sql);
		list = query.list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public ProgrammeVO getProgrammeById(int programmeId) {
		String sql = "select new com.vo.ProgrammeVO(a.programmeId, a.programmeName, a.description, a.isActive, a.contact.mobilePhone) "
				+ " from Programme as a where programmeId=" + programmeId;
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Query query = session.createQuery(sql);
		List<ProgrammeVO> List = query.list();
		transaction.commit();
		return List.get(0);
	}

	public boolean checkProgrammeName(String programmeName) {
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		String sql = "from Programme where programmeName='" + programmeName
				+ "'";
		Query query = session.createQuery(sql);
		List<Programme> list = query.list();
		if (list.size() > 0) {
			return false;
		}
		return true;

	}

	// public Programme load(int programmeId) {
	//
	// String sql = "from Programme WHERE programmeId =" + programmeId;
	// Session session = HibernateListener.getSessionFactory()
	// .getCurrentSession();
	// Transaction transaction = session.beginTransaction();
	// Query query = session.createQuery(sql);
	// List<Programme> ProgrammeList = query.list();
	// transaction.commit();
	// return ProgrammeList.get(0);
	// }

	public void deleteProgramme(int programmeId) {
		ProgrammeVO p = getProgrammeById(programmeId);
		Contact c = new ContactDAO().getContactByContactNumber(p
				.getContactNumber());
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Programme p1 = new Programme();
		p1.setProgrammeId(programmeId);
		p1.setProgrammeName(p.getProgrammeName());
		p1.setDescription(p.getDescription());
		p1.setContact(c);
		p1.setIsActive(p.getIsActive());
		session.delete(p1);
		transaction.commit();
		session.close();

	}

	public void insertProgramme(Programme p) {
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(p);
		transaction.commit();
		session.close();
	}

	public void updateProgramme(ProgrammeVO pvo, int programmeId) {
		Session session = HibernateListener.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		Programme p = new Programme();
		p = (Programme) session.get(Programme.class, programmeId);
		transaction.commit();
	
		session.close();
		p.setProgrammeName(pvo.getProgrammeName());
		p.setDescription(pvo.getDescription());
		p.setIsActive(pvo.getIsActive());
		p.setContact(new ContactDAO().getContactByContactNumber(pvo
				.getContactNumber()));
		Session session2 = HibernateListener.getSessionFactory().openSession();
		transaction = session2.beginTransaction();

		session2.update(p);
		transaction.commit();
		session2.close();
	
	}

}
